package cn.com.assistant.controller;

import cn.com.assistant.annotation.AdminLoginToken;
import cn.com.assistant.entity.CasAnswerEntity;
import cn.com.assistant.mapper.AnswerMapper;
import cn.com.assistant.mapper.CasAnswerMapper;
import cn.com.assistant.model.CasAnswerModel;
import cn.com.assistant.model.ImageDTO;
import cn.com.assistant.service.AnswerService;
import cn.com.assistant.service.CasAnswerService;
import cn.com.assistant.utils.ResponseMessage;
import cn.com.assistant.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/casAnswer")
public class CasAnswerController {
 
    @Autowired
    AnswerService answerService;
    @Autowired
    AnswerMapper answerMapper;
    @Autowired
    CasAnswerService casAnswerService;
    @Autowired
    CasAnswerMapper casAnswerMapper;

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/insert")
    @ApiOperation(value = "添加问题子回复", notes = "添加问题子回复")
    @ApiImplicitParam(name = "casAnswerModel", value = "问题子回复实体", required = true, dataType = "CasAnswerModel")
    public ResponseMessage insert(@RequestBody @Valid CasAnswerModel casAnswerModel, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        if (null != casAnswerModel && CollectionUtils.isNotEmpty(casAnswerModel.getCasAnswerEntities())) {
            List<CasAnswerEntity> casAnswerEntities = casAnswerModel.getCasAnswerEntities();
            Date date = new Date();
            for (int i = 0; i < casAnswerEntities.size(); i++) {
                CasAnswerEntity casAnswerEntity = casAnswerEntities.get(i);
                casAnswerEntity.setId(UUIDUtil.getUUID());
                casAnswerEntity.setCreatedDate(date);
                // 处理多张图片，追加分号
                StringBuffer imgUrl = null;
                List<String> imgUrlList = casAnswerEntity.getImgUrlList();
                if (CollectionUtils.isNotEmpty(imgUrlList)) {
                    for (int j = 0; j < imgUrlList.size(); j++) {
                        String url = imgUrlList.get(j) + ";";
                        imgUrl.append(url);
                    }
                }
                casAnswerEntity.setImgUrl(String.valueOf(imgUrl));
            }
            boolean flag = casAnswerService.saveBatch(casAnswerEntities);
            if (flag) {
                return ResponseMessage.defaultResponse().setMsg("保存成功！");
            } else {
                return ResponseMessage.failResponse().setMsg("保存失败！");
            }
        }
        return ResponseMessage.failResponse().setMsg("保存失败！");
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/update")
    @ApiOperation(value = "修改二级问题回复", notes = "修改二级问题回复")
    @ApiImplicitParam(name = "casAnswerEntity", value = "二级问题回复实体", required = true, dataType = "CasAnswerEntity")
    public ResponseMessage update(@RequestBody @Valid CasAnswerEntity casAnswerEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        casAnswerEntity.setUpdatedDate(new Date());
        boolean flag = casAnswerService.updateById(casAnswerEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/updateImage")
    @ApiOperation(value = "更新二级问题回复图片", notes = "更新一级问题回复图片")
    @ApiImplicitParam(name = "imageDTO", value = "更新图片模型", required = true, dataType = "ImageDTO")
    public ResponseMessage updateImage(@RequestBody @Valid ImageDTO imageDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        return casAnswerService.updateImage(imageDTO);
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除单条二级问题回复", notes = "删除单条二级问题回复")
    @ApiImplicitParam(name = "二级问题回复id", value = "二级问题回复id")
    public ResponseMessage delete(@RequestParam("id") String id) {

        boolean flag = casAnswerService.removeById(id);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("删除成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("删除失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/findByPId")
    @ApiOperation(value = "查询二级回复数据", notes = "查询二级回复数据")
    @ApiImplicitParam(name = "关联id", value = "关联id")
    ResponseMessage findByPid(@RequestParam("principalId") String principalId) {
        return casAnswerService.findByPid(principalId);
    }


}